home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.06 Jun 93 / Programmers' Challenge / rotate.c.jeff next >
Encoding:
Text File  |  1993-04-12  |  1.7 KB  |  60 lines  |  [TEXT/KAHL]

  1. //*********************************************************
  2. // RotateBitMapClockwise
  3. // By Jeff Mallett
  4. //
  5. // Rotates the significant bits of the srcBitMapPtr 90°
  6. // clockwise and stores the result in dstBitMapPtr.
  7. //*********************************************************
  8.  
  9. #define kHighShortBit        0x8000
  10. #define kBitsPerShort        16
  11.  
  12. // Creates a short from bits in different rows of
  13. // the source bitmap.  Then copies this short into
  14. // the destination bitmap.
  15. #define COPY_TWO_BYTES(stopValue)            \
  16.     data = 0, bit = kHighShortBit;            \
  17.     do {                                    \
  18.         if (*srcPos & srcBit) data |= bit;    \
  19.         srcPos += srcRowShorts;                \
  20.     } while ( (bit >>= 1) != stopValue );    \
  21.     *(dstPos++) = data
  22.  
  23. void RotateBitMapClockwise(BitMap *srcBitMapPtr,
  24.     BitMap *dstBitMapPtr)
  25. {
  26.     register unsigned short data, *srcPos, bit;
  27.     register int j;
  28.     register unsigned short srcBit, *dstPos, stopBit;
  29.     register int i;
  30.     unsigned short *baseSrcPtr;
  31.     // shorts per row of source
  32.     const int srcRowShorts =
  33.       srcBitMapPtr->rowBytes >> 1;
  34.     const int numSrcRows =
  35.       srcBitMapPtr->bounds.bottom - srcBitMapPtr->bounds.top;
  36.     
  37.     dstPos = (unsigned short *)dstBitMapPtr->baseAddr;
  38.     srcPos = baseSrcPtr =
  39.       (unsigned short *)srcBitMapPtr->baseAddr;
  40.     srcBit = kHighShortBit;
  41.     
  42.     for (i = srcBitMapPtr->bounds.right -
  43.       srcBitMapPtr->bounds.left; i; --i) {
  44.         // Copy one column of source to a row of destination
  45.         for (j = numSrcRows / kBitsPerShort; j; --j) {
  46.             COPY_TWO_BYTES(0);
  47.         }
  48.         if (j = numSrcRows % kBitsPerShort) {
  49.             stopBit = kHighShortBit >> j;
  50.             COPY_TWO_BYTES(stopBit); // Final 2 bytes
  51.         }
  52.         // Prepare to copy next column of source
  53.         if ( !(srcBit >>= 1) ) {
  54.             srcBit = kHighShortBit;
  55.             ++baseSrcPtr;
  56.         }
  57.         srcPos = baseSrcPtr;
  58.     }
  59. }
  60.